package com.example.othree.controller;

import com.example.othree.common.R;
import com.example.othree.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangmiaohang
 * @since 2024-12-28
 */
@RestController
@CrossOrigin
@RequestMapping("/api")
public class OzoneCaseController {

    @Autowired
    PollutionCaseHourService pollutionCaseHourService;

    @Autowired
    PollutionCaseDayService pollutionCaseDayService;

    @Autowired
    WeatherCaseDayService weatherCaseDayService;

    @Autowired
    PollutionHistoryDataJjjSitesDayService pollutionHistoryDataJjjSitesDayService;

    @Autowired
    PollutionHistoryDataJjjSitesService pollutionHistoryDataJjjSitesService;

    private final String[] stations = {"1080A", "1075A", "1030A", "1060A", "1065A", "1037A", "1056A", "1073A", "1044A"};
    private static final Map<String, String> cityForStation;
    static {
        cityForStation = new HashMap<>();
        cityForStation.put("邢台", "1080A");
        cityForStation.put("衡水", "1075A");
        cityForStation.put("石家庄", "1030A");
        cityForStation.put("张家口", "1060A");
        cityForStation.put("承德", "1065A");
        cityForStation.put("唐山", "1037A");
        cityForStation.put("保定", "1056A");
        cityForStation.put("沧州", "1073A");
        cityForStation.put("秦皇岛", "1044A");

        cityForStation.put("廊坊", "1065A");
        cityForStation.put("邯郸", "1065A");
    }
    @GetMapping("/hebei-O3-Pollution-data")
    @ApiOperation("获取某臭氧污染个例污染持续过程中的臭氧浓度数据")
    public R pollutionDataCountAllByDate(
                                         @RequestParam(value = "dataType", defaultValue = "逐日") String dataType,
                                         @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime){
        HashMap<String, Object> result = new HashMap<>();
        try{
            if ("逐时".equals(dataType)){
                for (HashMap<String, String> i : pollutionCaseHourService.OzoneCaseHourForTime(startTime, endTime)){
                    Date date = (Date)((Object)i.remove("date"));
                    String time = new SimpleDateFormat("yyyy-MM-dd HH").format(date);

                    if (!result.containsKey(time)){
                        HashMap<String, HashMap<String, String>> timeData = new HashMap<>();
                        result.put(time, timeData);
                    }

                    String city = i.remove("city");
                    HashMap<String, Object> data = new HashMap<>();
                    for (String j : i.keySet()){
                        if ("PM2_5".equals(j)) data.put("PM2.5", i.get(j));
                        else data.put(j, i.get(j));
                    }
                    HashMap<String, Object> timeData = (HashMap<String, Object>) result.get(time);
                    timeData.put(city, data);
                }
            }else if ("逐日".equals(dataType)){
                for (HashMap<String, String> i : pollutionCaseDayService.OzoneCaseDayForTime(startTime, endTime)){
                    Date date = (Date)((Object)i.remove("date"));
                    String time = new SimpleDateFormat("yyyy-MM-dd").format(date);

                    if (!result.containsKey(time)){
                        HashMap<String, HashMap<String, String>> timeData = new HashMap<>();
                        result.put(time, timeData);
                    }

                    String city = i.remove("city");
                    HashMap<String, Object> data = new HashMap<>();
                    for (String j : i.keySet()){
                        if ("PM2_5".equals(j)) data.put("PM2.5", i.get(j));
                        else data.put(j, i.get(j));
                    }
                    HashMap<String, Object> timeData = (HashMap<String, Object>) result.get(time);
                    timeData.put(city, data);
                }
            }

        } catch (DataAccessException e) {
            e.printStackTrace();
            return R.error().message("数据库访问出错");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("出现了未知错误");
        }
        return R.ok().data("data", result);
    }
    @GetMapping("/hebei-O3-Weather-data")
    @ApiOperation("返回某臭氧污染个例库中对应城市在一次污染过程时间范围的气象要素。")
    public R weatherDataCountAllByDate(
            @RequestParam("city") String city,
            @RequestParam("dataType") String dataType,
            @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime){
        List<HashMap<String, String>> result = null;
        try{
           result = weatherCaseDayService.OzoneCaseDayForTime(startTime, endTime, city);

        } catch (DataAccessException e) {
            e.printStackTrace();
            return R.error().message("数据库访问出错");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("出现了未知错误");
        }
        return R.ok().data("data", result);
    }
}
